/**
 * 实际费用核销 - 编辑页面
 * @author mengyb
 * @date 2019/10/25
 */
define(function () {

    var self,
        app = win.app,
        doc = document,
        layui = win.layui,
        vueData = {
            isAdd: false,
            isCheck:false,
            form: '',
            rules: {
                GWFEDOCNO: [
                    {required: true, message: '不可为空', trigger: 'blur'}
                ],
                GWFE001: [
                    {required: true, message: '不可为空', trigger: 'blur'}
                ],
                GWFE002: [
                    {required: true, message: '不可为空', trigger: 'change'}
                ],
            },
            total: {
                GWFE103_SUM:0,
                GWFE105_SUM:0
            }
        },
        cols = [[
            {type:'checkbox',fixed: 'left'},
            {field: "GWFESEQ", title: "项次",width: 80},
            {field: "GWFE009", title: "实际费用计算单号",width: 150,event:'openJs'},
            {field: "GWFE010", title: "项次",width: 80},
            {field: "GWFE003", title: "区域",width: 80,hide:true},
            {field: "GWFE003_NAME", title: "区域",width: 80,event:'openQy'},
            {field: "GWFE004", title: "客户",width: 150,hide:true},
            {field: "GWFE004_NAME", title: "客户",width: 150,event:'openKh'},
            {field: "GWFE005", title: "品类",width: 120,hide:true},
            {field: "GWFE005_NAME", title: "品类",width: 120,event:'openPl'},
            {field: "GWFE006", title: "业务组织",width: 100,hide:true},
            {field: "GWFE006_NAME", title: "业务组织",width: 100,event:'openZz'},
            {field: "GWFE007", title: "费用",width: 100,hide:true},
            {field: "GWFE007_NAME", title: "费用",width: 100,event:'openFy'},
            {field: "GWFE008", title: "费用核销方式",width: 100,edit:'select',
                filterDatas: [{code:'',value:'请选择'}].concat(app.datas_('crm_gwft400_gwfe008'))
            },
            {field: "GWFE100", title: "币别",width: 100,hide:true},
            {field: "GWFE100_NAME", title: "币别",width: 100},
            {field: "GWFE101", title: "汇率",width: 100},
            {field: "GWFE102", title: "区域核销费用原币金额",width: 150,edit:'text'},
            {field: "GWFE103", title: "区域核销费用本币金额",width: 150},
            {field: "GWFE106", title: "区域核销差异原因",width: 150,edit:'text'},
            {field: "GWFE104", title: "总部核销费用原币金额",width: 150,edit:'text'},
            {field: "GWFE105", title: "总部核销费用本币金额",width: 150},
            {field: "GWFE107", title: "总部核销差异原因",width: 150,edit:'text'},
            {field: "GWFEACTI", title: "审核码",width: 100,filterDatas: app.datas_('check_code')},
            {field: "GWFD106", title: "区域实际费用原币金额",width: 150,hide:true},
            {field: "GWFD107", title: "区域实际费用本币金额",width: 150,hide:true},
            {field: "GWFD108", title: "总部实际费用原币金额",width: 150,hide:true},
            {field: "GWFD109", title: "总部实际费用本币金额",width: 150,hide:true}
        ]],
        digit={
            rate:100,
            price:100,
            amount:100
        },
        callObj,
        listData = null,
        table,
        gwft400Table,
        pageVue,
        layOpenIndex
    ;

    var Gwft400Edit = app.Module.extend({
            init: function () {
                self = this;
                self._super({
                    _tag: 'gwft400_edit',
                    _loadTplsFull: [{
                        dom: 'common_open_window', tpl: 'common/_open_window'
                    }]
                }, win);
            },

            initTable: function () {
                layui.use(['table'], function () {
                    table = layui.table;
                    gwft400Table = table.render({
                        elem: app.getByid(doc, 'gwft400_edit_table'),
                        id: 'gwft400_edit_table',
                        cols: cols,
                        height:'250',
                        page: false
                    });

                    //监听单元格编辑
                    table.on('edit(gwft400_edit_table)', function (obj) {
                        if(obj.data.GWFE009 == '' || obj.data.GWFE009 == null){
                            app.showError('请先选择实际费用计算单号！')
                            return;
                        }
                        if (obj.field == 'GWFE101') {
                            if (!(/^\d+(\.\d+)?$/).test(obj.value)) {
                                obj.updateOfEdit({
                                    'GWFE101': 0,
                                    'GWFE103': 0,
                                    'GWFE105': 0
                                });
                                app.showError('汇率必须为正数');
                            } else {
                                if(obj.data.GWFE102 != '' && obj.data.GWFE102 != null){
                                    obj.updateOfEdit({
                                        'GWFE101': obj.value,
                                        'GWFE103': self.numConversion(obj.value*obj.data.GWFE102,digit.amount)
                                    });
                                }else if(obj.data.GWFE104 !='' && obj.data.GWFE104 != null){
                                    obj.updateOfEdit({
                                        'GWFE101': obj.value,
                                        'GWFE105': self.numConversion(obj.value*obj.data.GWFE104,digit.amount)
                                    });
                                } else{
                                    obj.updateOfEdit({
                                        'GWFE101': obj.value,
                                    });
                                }
                            }
                            self.calTotal();
                        }else if(obj.field == 'GWFE102'){
                            if(obj.value > obj.data.GWFD106){
                                obj.updateOfEdit({
                                    'GWFE102': 0,
                                    'GWFE103': 0
                                });
                                app.showError('核销金额不可超过计算金额');
                            }else if (!(/^\d+(\.\d+)?$/).test(obj.value)) {
                                obj.updateOfEdit({
                                    'GWFE102': 0,
                                    'GWFE103': 0
                                });
                                app.showError('区域原币金额不可以小于0');
                            } else {
                                if(obj.data.GWFE101 != '' && obj.data.GWFE101 != null){
                                    obj.updateOfEdit({
                                        'GWFE102': obj.value,
                                        'GWFE103': self.numConversion(obj.value*obj.data.GWFE101,digit.amount)
                                    });
                                }else{
                                    obj.updateOfEdit({
                                        'GWFE102': obj.value,
                                    });
                                }
                            }
                            self.calTotal();
                        }else if(obj.field == 'GWFE103'){
                            if(obj.value > obj.data.GWFD106){
                                obj.updateOfEdit({
                                    'GWFE103': 0
                                });
                                app.showError('核销金额不可超过计算金额');
                            }else if (!(/^\d+(\.\d+)?$/).test(obj.value)) {
                                obj.updateOfEdit({
                                    'GWFE103': 0
                                });
                                app.showError('区域本币金额不可以小于0');
                            }
                            self.calTotal();
                        }else if(obj.field == 'GWFE104'){
                            if(obj.value > obj.data.GWFD108){
                                obj.updateOfEdit({
                                    'GWFE104': 0,
                                    'GWFE105': 0
                                });
                                app.showError('核销金额不可超过计算金额');
                            }else if (!(/^\d+(\.\d+)?$/).test(obj.value)) {
                                obj.updateOfEdit({
                                    'GWFE104': 0,
                                    'GWFE105': 0
                                });
                                app.showError('总部原币金额不可以小于0');
                            } else {
                                if(obj.data.GWFE101 != '' && obj.data.GWFE101 != null){
                                    obj.updateOfEdit({
                                        'GWFE104': obj.value,
                                        'GWFE105': self.numConversion(obj.value*obj.data.GWFE101,digit.amount)
                                    });
                                }else{
                                    obj.updateOfEdit({
                                        'GWFE104': obj.value,
                                    });
                                }
                            }
                            self.calTotal();
                        }else if(obj.field == 'GWFE105'){
                            if(obj.value > obj.data.GWFD109){
                                obj.updateOfEdit({
                                    'GWFE105': 0
                                });
                                app.showError('核销金额不可超过计算金额');
                            }else if (!(/^\d+(\.\d+)?$/).test(obj.value)) {
                                obj.updateOfEdit({
                                    'GWFE105': 0
                                });
                                app.showError('总部本币金额不可以小于0');
                            }
                        }
                        self.calTotal();
                    });

                    //监听单元格单击事件
                    table.on('tool(gwft400_edit_table)', function (obj) {
                        if (obj.event == 'openQy') {
                            if(obj.data.GWFE009 == '' || obj.data.GWFE009 == null){
                                app.showError('请先选择实际费用计算单号！');
                                return;
                            }
                            self._openTplFull({
                                tag: 'common_open_window',
                                tagData: {
                                    config: {
                                        method: 'q_gwad002',
                                        columnList: [
                                            {label: '区域编号', prop: 'GWAD002', width: '150'},
                                            {label: '区域名称', prop: 'GWADL004', width: '150'},
                                            {label: '助记码', prop: 'GWADL005', width: '150'}
                                        ]
                                    },
                                    cb: function (data) {
                                        obj.updateOfEdit({
                                            'GWFE003': data.GWAD002,
                                            'GWFE003_NAME': data.GWADL004,
                                        });
                                    }
                                },
                                index: 0,
                                openOpts: {
                                    title: '选择区域',
                                    area: ['600px', '470px']
                                }
                            });
                        }else if (obj.event == 'openPl') {
                            if(obj.data.GWFE009 == '' || obj.data.GWFE009 == null){
                                app.showError('请先选择实际费用计算单号！');
                                return;
                            }
                            self._openTplFull({
                                tag: 'common_open_window',
                                tagData: {
                                    config: {
                                        method: 'q_gwao001',
                                        columnList: [{label: '产品系列编号', prop: 'GWAO001', width: '150'},
                                            {label: '说明', prop: 'GWAOL003', width: '150'},
                                            {label: '助记码', prop: 'GWAOL004', width: '150'}
                                        ]
                                    },
                                    cb: function (data) {
                                        obj.updateOfEdit({
                                            'GWFE005': data.GWAO001,
                                            'GWFE005_NAME': data.GWAOL003,
                                        });
                                    }
                                },
                                index: 0,
                                openOpts: {
                                    title: '选择品类',
                                    area: ['600px', '465px']
                                }
                            });
                        }else if(obj.event == 'openZz'){
                            if(obj.data.GWFE009 == '' || obj.data.GWFE009 == null){
                                app.showError('请先选择实际费用计算单号！');
                                return;
                            }
                            self._openTplFull({
                                tag: 'common_open_window',
                                tagData: {
                                    config: {
                                        method:'q_sysorg_id',
                                        columnList: [
                                            {label: '组织编号', prop: 'ID', width: '150'},
                                            {label: '组织名称', prop: 'NAME', width: '150'}
                                        ],
                                        searchParam: {
                                            TYPE:'3'
                                        }
                                    },
                                    cb: function (data) {
                                        obj.updateOfEdit({
                                            'GWFE006': data.ID,
                                            'GWFE006_NAME': data.NAME,
                                        });
                                    }
                                },
                                index: 0,
                                openOpts: {
                                    title: '选择组织',
                                    area: ['600px', '465px']
                                }
                            });
                        }else if(obj.event == 'openKh'){
                            if(obj.data.GWFE009 == '' || obj.data.GWFE009 == null){
                                app.showError('请先选择实际费用计算单号！');
                                return;
                            }
                            self._openTplFull({
                                tag: 'common_open_window',
                                tagData: {
                                    config: {
                                        method:'q_gwak001',
                                        columnList: [
                                            {label: '客户编号', prop: 'GWAK001', width: '150'},
                                            {label: '客户简称', prop: 'GWAKL003', width: '150'},
                                            {label: '客户全称', prop: 'GWAKL004', width: '150'}
                                        ]
                                    },
                                    cb:function (data) {
                                        obj.updateOfEdit({
                                            'GWFE004': data.GWAK001,
                                            'GWFE004_NAME': data.GWAKL004,
                                            'GWFE100': data.GWAK008,
                                            'GWFE100_NAME': data.GWAK008_NAME
                                        });
                                        app.ajax({
                                            type:'POST',
                                            dataType:'json',
                                            xurl:'crm/gwft400/getRate',
                                            xdata:{
                                                CURRENCY:data.GWAK008,
                                                DOCDATE:vueData.form.GWFEDOCDATE
                                            },
                                            cb: function (result) {
                                                if(result.ok){
                                                    if(result.data.data.RATE != 'N'){
                                                        var GWFE101 = self.numConversion(result.data.data.RATE,digit.rate)
                                                        obj.updateOfEdit({
                                                            'GWFE101': GWFE101
                                                        });
                                                    }
                                                }
                                            }
                                        });
                                    }
                                },
                                index:0,
                                openOpts: {
                                    title: '选择客户',
                                    area:['600px', '465px']
                                }
                            });
                        }else if(obj.event == 'openFy'){
                            if(obj.data.GWFE009 == '' || obj.data.GWFE009 == null){
                                app.showError('请先选择实际费用计算单号！');
                                return;
                            }
                            self._openTplFull({
                                tag: 'common_open_window',
                                tagData: {
                                    config: {
                                        method:'q_gwfa001',
                                        columnList: [
                                            {label: '费用编号', prop: 'GWFA001', width: '150'},
                                            {label: '费用说明', prop: 'GWFAL003', width: '150'},
                                        ]
                                    },
                                    cb:function (data) {
                                        obj.updateOfEdit({
                                            'GWFE007': data.GWFA001,
                                            'GWFE007_NAME': data.GWFAL003,
                                        });
                                    }
                                },
                                index:0,
                                openOpts: {
                                    title: '选择费用',
                                    area:['600px', '465px']
                                }
                            });
                        }else if(obj.event == 'openJs'){
                            var list =gwft400Table .getDatas();
                            var keyArray=[];
                            for(var i =0;i<list.length;i++){
                                if(list[i]['GWFE009'] != null && list[i]['GWFE009'] !=null){
                                    keyArray.push(
                                        list[i]['GWFE009']+'-'+list[i]['GWFE010']
                                    );
                                }
                            }
                            var keyArrayStr=keyArray.join(',');
                            self._openTplFull({
                                tag: 'common_open_window',
                                tagData: {
                                    multiple:true,
                                    config: {
                                        method:'q_gwfd001',
                                        columnList: [
                                            {label: '实际费用计算单号', prop: 'GWFDDOCNO', width: '180'},
                                            {label: '项次', prop: 'GWFDSEQ', width: '80'},
                                            {label: '区域', prop: 'GWFD003_NAME', width: '80'},
                                            {label: '客户', prop: 'GWFD004_NAME', width: '150'},
                                            {label: '品类', prop: 'GWFD005_NAME', width: '150'},
                                            {label: '业务组织', prop: 'GWFD006_NAME', width: '150'},
                                            {label: '费用', prop: 'GWFD007_NAME', width: '150'},
                                        ],
                                        dataList:[],
                                        key:'DOCNO_SEQ',
                                        searchParam:{
                                            keyArrayStr:keyArrayStr
                                        }
                                    },
                                    cb:function (data) {
                                        obj.updateOfEdit({
                                            'GWFE009': data[0].GWFDDOCNO,
                                            'GWFE010': data[0].GWFDSEQ,
                                            'GWFE003': data[0].GWFD003,
                                            'GWFE003_NAME': data[0].GWFD003_NAME,
                                            'GWFE004': data[0].GWFD004,
                                            'GWFE004_NAME': data[0].GWFD004_NAME,
                                            'GWFE005': data[0].GWFD005,
                                            'GWFE005_NAME': data[0].GWFD005_NAME,
                                            'GWFE006': data[0].GWFD006,
                                            'GWFE006_NAME': data[0].GWFD006_NAME,
                                            'GWFE007': data[0].GWFD007,
                                            'GWFE007_NAME': data[0].GWFD007_NAME,
                                            'GWFE100':data[0].GWFD100,
                                            'GWFE100_NAME':data[0].GWFD100_NAME,
                                            'GWFE101':data[0].GWFD101,
                                            'GWFD106': data[0].GWFD106,
                                            'GWFD107': data[0].GWFD107,
                                            'GWFD108': data[0].GWFD108,
                                            'GWFD109': data[0].GWFD109
                                        });
                                        listData = gwft400Table.getDatas();
                                        var count = 0;
                                        if(listData.length > 0){
                                            count = listData[listData.length-1].GWFESEQ;
                                        }
                                        for(var i=1;i<data.length;i++){
                                            listData.push({
                                                GWFESEQ:count+1,
                                                GWFE009:data[i].GWFDDOCNO,
                                                GWFE010:data[i].GWFDSEQ,
                                                GWFE003:data[i].GWFD003,
                                                GWFE003_NAME:data[i].GWFD003_NAME,
                                                GWFE004:data[i].GWFD004,
                                                GWFE004_NAME:data[i].GWFD004_NAME,
                                                GWFE005:data[i].GWFD005,
                                                GWFE005_NAME:data[i].GWFD005_NAME,
                                                GWFE006:data[i].GWFD006,
                                                GWFE006_NAME:data[i].GWFD006_NAME,
                                                GWFE007:data[i].GWFD007,
                                                GWFE007_NAME:data[i].GWFD007_NAME,
                                                GWFE008:null,
                                                GWFE100:data[i].GWFD100,
                                                GWFE100_NAME:data[i].GWFD100_NAME,
                                                GWFE101:data[i].GWFD101,
                                                GWFE102:0,
                                                GWFE103:0,
                                                GWFE104:0,
                                                GWFE105:0,
                                                GWFE106:null,
                                                GWFE107:null,
                                                GWFEACTI:'N',
                                                // GWFEDOCNO:'',
                                                // GWFEDOCDATE:'',
                                                // GWFE001:'',
                                                // GWFE002:''
                                                GWFD106:data[i].GWFD106,
                                                GWFD107:data[i].GWFD107,
                                                GWFD108:data[i].GWFD108,
                                                GWFD109:data[i].GWFD109
                                            });
                                            self.tableReload();
                                        }

                                    }
                                },
                                index:0,
                                openOpts: {
                                    title: '选择实际费用计算单号',
                                    area:['600px', '500px']
                                }
                            });
                        }
                    });
                });
            },

            tableReload: function () {
                gwft400Table.reload({
                    data: listData
                });
            },

            add: function (listData) {
                app.ajax({
                    xurl: 'crm/gwft400/add',
                    xdata: listData,
                    loading: true,
                    toJsonStr: true,
                    okmsg: self._moduleName + '增加成功',
                    cb: function (result) {
                        if (result.ok) {
                            app.pageModule('gwft400').reloadTableCurr();
                            self._closeTpl();
                        }
                    }
                });
            },

            edit: function (listData) {
                app.ajax({
                    xurl: 'crm/gwft400/edit',
                    xdata: listData,
                    loading: true,
                    toJsonStr: true,
                    okmsg: self._moduleName + '修改成功',
                    cb: function (result) {
                        if (result.ok) {
                            app.pageModule('gwft400').reloadTableCurr();
                            self._closeTpl();
                        }
                    }
                });
            },

            getDigit:function() {
                app.ajax({
                    xurl: 'admin/getDigit',
                    async: true,
                    cb: function (result) {
                        if(result.ok) {
                            var data = result.data.data;
                            var _digit = {
                                rate: Math.pow(10,data.rate),
                                price: Math.pow(10,data.price),
                                amount: Math.pow(10,data.amount),

                            }
                            app.extendUpLow({
                                target: digit,
                                res: _digit,
                                reset: true
                            })
                            console.log(_digit)
                        }else {
                            console.log("获取小数位失败");
                        }
                    }
                });
            },

            numConversion:function (num,digit) {
                return  math.div(Math.round(math.mul(num, digit)),digit);
            },

            calTotal(){
                var list = gwft400Table.getDatas();
                var sum1 = 0,sum2=0;
                for(var i=0;i<list.length;i++){
                    sum1 = math.add(sum1, list[i].GWFE103);
                    sum2 = math.add(sum2, list[i].GWFE105);
                }
                vueData.total.GWFE103_SUM = sum1;
                vueData.total.GWFE105_SUM = sum2;
            },

            _call: function (tagData) {
                pageVue.$refs['form'].resetFields();
                if (tagData.isCheck) {
                    vueData.isCheck = true;
                } else {
                    vueData.isCheck = false;
                }
                if (tagData.isAdd) {
                    vueData.isAdd = true;
                    app.ajax({
                        xurl: 'crm/gwft400/getAddDef',
                        cb: function (result) {
                            if (result.ok) {
                                app.extendUpLow({
                                    target: vueData.form,
                                    res: {
                                        GWFEDOCNO:result.data.data.GWFEDOCNO,
                                        GWFEDOCDATE:result.data.data.GWFEDOCDATE
                                    },
                                    reset: true
                                });
                                listData = [];
                                self.tableReload();
                            }
                        }
                    });
                } else {
                    vueData.isAdd = false;
                    callObj = tagData.obj;
                    app.extendUpLow({
                        target: vueData.form,
                        res: callObj.data,
                        reset: true
                    });
                    app.ajax({
                        xurl: 'crm/gwft400/getSingleData',
                        xdata: {GWFEDOCNO: callObj.data.GWFEDOCNO},
                        loading: true,
                        cb: function (result) {
                            listData = result.data.data;
                            self.tableReload();
                            self.calTotal();
                        }
                    });
                }
            },

            _ready: function () {
                table = layui.table;
                vueData.form = app.fields_(['GWFE_T']);
                pageVue = new Vue({
                    el: '#gwft400_edit',
                    data: vueData,
                    watch: {},
                    methods: {
                        addRow: function () {
                            if (vueData.form.GWFE001 == '' || vueData.form.GWFE001 == null) {
                                app.showError("请先选择年度！");
                                return;
                            }
                            if (vueData.form.GWFE002 == '' || vueData.form.GWFE002 == null) {
                                app.showError("请先选择月份！");
                                return;
                            }
                            listData = gwft400Table.getDatas();
                            var count = 0;
                            if(listData.length > 0){
                                count = listData[listData.length-1].GWFESEQ;
                            }
                            listData.push({
                                GWFESEQ:count+1,
                                GWFE009:null,
                                GWFE010:null,
                                GWFE003:null,
                                GWFE004:null,
                                GWFE005:null,
                                GWFE006:null,
                                GWFE007:null,
                                GWFE008:null,
                                GWFE100:null,
                                GWFE101:1,
                                GWFE102:0,
                                GWFE103:0,
                                GWFE104:0,
                                GWFE105:0,
                                GWFE106:null,
                                GWFE107:null,
                                GWFEACTI:'N',
                                // GWFEDOCNO:'',
                                // GWFEDOCDATE:'',
                                // GWFE001:'',
                                // GWFE002:''
                                GWFD106:0,
                                GWFD107:0,
                                GWFD108:0,
                                GWFD109:0
                            });
                            self.tableReload();
                        },
                        delRow: function () {
                            var tableDatas = gwft400Table.getDatas();
                            var checkStatus = table.checkStatus('gwft400_edit_table');
                            if (checkStatus.data.length > 0) {
                                checkStatus.data.forEach(function (v) {
                                    tableDatas.removeByMap({
                                        GWFESEQ: v.GWFESEQ
                                    });
                                });
                                var count=1;
                                tableDatas.forEach(function (item) {
                                    item.GWFESEQ=count++;
                                })
                                listData = tableDatas;
                                self.tableReload();
                            }
                        },
                        cancle: function () {
                            self._closeTpl();
                        },
                        save: function () {
                            this.$refs['form'].validate(function (valid) {
                                if (valid) {
                                    var list = gwft400Table.getDatas();
                                    if(list.length == 0){
                                        app.showError("表格内容不可为空！");
                                        return;
                                    }
                                    var flag=true;
                                    for(var i=0;i<list.length;i++){
                                        if(list[i].GWFE003 == '' || list[i].GWFE003 ==null ){
                                            flag = false;
                                            app.showError('区域不可为空！');
                                            return;
                                        }
                                        if(list[i].GWFE004 == '' || list[i].GWFE004 ==null ){
                                            flag = false;
                                            app.showError('客户不可为空！');
                                            return;
                                        }
                                        if(list[i].GWFE005 == '' || list[i].GWFE005 ==null ){
                                            flag = false;
                                            app.showError('品类不可为空！');
                                            return;
                                        }
                                        if(list[i].GWFE006 == '' || list[i].GWFE006 ==null ){
                                            flag = false;
                                            app.showError('业务组织不可为空！');
                                            return;
                                        }
                                        if(list[i].GWFE007 == '' || list[i].GWFE007 ==null ){
                                            flag = false;
                                            app.showError('费用不可为空！');
                                            return;
                                        }

                                    }
                                    if(flag){
                                        list.addMap(vueData.form, false);
                                        if (vueData.isAdd) {
                                            self.add(list);
                                        } else {
                                            self.edit(list);
                                        }
                                    }
                                }
                            });
                        }
                    },
                    created: function () {
                        self._setEleIsReady('gwft400_edit');
                    }
                });

                self.initTable();
            },
            _lazy: function () {
                this._super();
                self.getDigit();
            }
        })
    ;

    return new Gwft400Edit();

})
;
